package com.secxiot.common.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.secxiot.common.constant.ServerStatusEnum;
import com.secxiot.common.exception.BaseException;
import com.secxiot.common.exception.BaseRuntimeException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel
public class ResultVo<T> implements Serializable {

    private static final long serialVersionUID = 1889499574409607305L;

    @ApiModelProperty( value = "是否处理成功，是：true ,否:false")
    private boolean success = true;

    @ApiModelProperty( value = "状态码")
    private int status = 200;

    @ApiModelProperty( value = "状态消息")
    private String message = "处理成功";

    @JsonIgnore
    private ServerStatusEnum error ;

    @ApiModelProperty( value = "数据")
    private T data ;

    public ResultVo() {
    }

    public ResultVo(T data) {
        this.success = true;
        this.data = data;
    }
    public ResultVo(String message, boolean success,int status,T data) {
        this.success = success;
        this.data = data;
        this.status=status;
        this.message=message;
    }

    public ResultVo(ServerStatusEnum exEnum) {
        this.success = false;
        this.status = exEnum.getStatus();
        this.message = /*exEnum.getErrorCode() + "|" + */exEnum.getMessage();
        this.error = exEnum;
    }

    public ResultVo(Exception e) {
        this.success = false;
        if (e instanceof BaseRuntimeException) {
            BaseRuntimeException base = (BaseRuntimeException) e;
            this.status = base.getExEnum().getStatus();
            this.message = /*base.getExEnum().getErrorCode() + "|" +*/ base.getExEnum().getMessage();
            this.error = base.getExEnum();
        } else if(e instanceof BaseException) {
            BaseException base = (BaseException) e;
            this.status = base.getExEnum().getStatus();
            this.message = /*base.getExEnum().getErrorCode() + "|" +*/ base.getExEnum().getMessage();
            this.error = base.getExEnum();
        }else {
            this.status = ServerStatusEnum.SYSTEM_EXCEPTION.getStatus();
            this.message = /*ExceptionEnum.SYSTEM_EXCEPTION.getErrorCode() + "|" + */ServerStatusEnum.SYSTEM_EXCEPTION.getMessage();
            this.error = ServerStatusEnum.SYSTEM_EXCEPTION;
        }
    }

    public boolean getSuccess() {
        return success;
    }

    public int getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public ServerStatusEnum getError() {
        return error;
    }

    @Override
    public String toString() {
        return "ResultVo{" +
                "success=" + success +
                ", status='" + status + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}

